import { ARImageApi } from '@/unit/api';
import { defaultErrorHandler } from '@/unit/error-handler';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { Image } from './types';
interface PickState {
  isLoading: boolean;
  timer: number;
  list: Image[];
}
export const usePickStore = defineStore({
  id: 'PickStore',
  state: (): PickState => ({ isLoading: false, timer: 0, list: [] }),
  actions: {
    /**
     * 确认上传
     * @param lng
     * @param lat
     * @param url
     * @param userId
     */
    async queryUpload(lng: number, lat: number, url: string, userId: number) {
      const params = {
        lng,
        lat,
        url,
        userId,
      };
      try {
        this.isLoading = true;
        const result = await ARImageApi.apiImageUploadImgPost(params);

        if (result) {
          message.success('上传成功');
        }
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 获取列表
     */
    async getAllImage() {
      // console.log('query');
      try {
        this.isLoading = true;
        const result = await ARImageApi.apiImageGetallGet();
        if (result) {
          this.list = result as Image[];
          // console.log(this.list);
        }
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    setTimer(value: number) {
      this.timer = value;
    },
  },
});
